Skip to content

Conversation

@dimitrijjedich
Copy link
Collaborator

🗒️ General

The Counting Sort algorithm is implementated not by comparison but instead uses the range of elements in the input array to count occurrences, enabling the array to be sorted. The implementation is particularly efficient when the range of numbers in the array to sort is very small.

💡 Idea

Counting Sort operates by creating a helper array to store counts of individual elements. After that the array is accumulated to determine the positions of each element in the sorted array.

🤔 Details

  • Range Calculation: The algorithm computes the minimum and maximum elements of the input array to determine the range.
  • Helper Array: A helper array is initialized, covering the entire range of elements, with all values set to zero. The algorithm iterates over the input array, incrementing the count for each element.
  • Cumulative Sum: The helper array is modified to store cumulative sums, indicating the positions of elements in the sorted array.
  • Placement: Finally, the algorithm traverses the input array, placing each element in its correct sorted position, by retrieving the postion from the accumulated helper array.

📉 Downside

  • Since this sorting algorithm depends on the possibility to count and by that hit a element in the array, this algorithm only works with arrays of integers, therefore rendering this useless for all other cases
  • Not suitable for large datasets with large ranges compared to the number of elements, as the helper array size grows proportionally to the range.

🌎🚀🛰️OOS Stuff

This PR also adds the Class ArrayHelper. the added methods can be called staticly and are:

  • getMin
  • getMax
  • consistsOfInt
  • isSorted
    This methods may be implemented with custom code later on but for now just use simple PHP function to get the expected result

enhances the overall library by adding a robust, efficient sorting algorithm and complementing utilities in the ArrayHelper class.

@dimitrijjedich dimitrijjedich self-assigned this Dec 23, 2024
@sonarqubecloud
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants